﻿using System;
using System.Windows;
using WpfApp1.Services;
using WpfApp1.Views;

namespace WpfApp1;

public partial class MainWindow : Window
{
    private readonly IUserService _userService;
    private readonly IStudentService _studentService;
    private readonly ICourseService _courseService;
    private readonly ICourseSelectService _courseSelectService;
    private readonly ITeacherCourseService _teacherCourseService;
    
    public MainWindow(
        IUserService userService, 
        IStudentService studentService, 
        ICourseService courseService, 
        ICourseSelectService courseSelectService,
        ITeacherCourseService teacherCourseService)
    {
        InitializeComponent();
        _userService = userService ?? throw new ArgumentNullException(nameof(userService));
        _studentService = studentService ?? throw new ArgumentNullException(nameof(studentService));
        _courseService = courseService ?? throw new ArgumentNullException(nameof(courseService));
        _courseSelectService = courseSelectService ?? throw new ArgumentNullException(nameof(courseSelectService));
        _teacherCourseService = teacherCourseService ?? throw new ArgumentNullException(nameof(teacherCourseService));
    }

    private void LoginButton_Click(object sender, RoutedEventArgs e)
    {
        string username = UsernameTextBox.Text;
        string password = PasswordBox.Password;
        if (String.IsNullOrEmpty(username) || String.IsNullOrEmpty(password))
        {
            MessageBox.Show("用户名或密码为空。");
            return;
        }
        
        var user = _userService.AuthenticateUser(username, password);
        
        if (user != null)
        {
            if (user.Role == 1)
            {
                var adminDashboard = new AdminDashboard(_studentService, _courseService, _courseSelectService);
                adminDashboard.Show();
            }
            else if (user.Role == 2)
            {
                var teacherDashboard = new TeacherDashboard(
                    username,  // 使用教师工号作为teacherNumber
                    _teacherCourseService,
                    _courseSelectService
                );
                teacherDashboard.Show();
            }
            else if (user.Role == 3)
            {
                // 验证学生是否存在
                var student = _studentService.GetStudentByNumber(username);
                if (student == null)
                {
                    MessageBox.Show("该学号不存在！");
                    return;
                }
                
                var studentDashboard = new StudentDashboard(
                    username, 
                    _studentService, 
                    _courseService, 
                    _courseSelectService
                );
                studentDashboard.Show();
            }
            // 关闭登录窗口
            this.Close();
        }
        else
        {
            MessageBox.Show("用户名或密码无效。");
        }
    }

    private void CloseButton_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }
}